﻿#region Copyright (c) 2009 S. van Deursen
/* The CuttingEdge.EntitySorting library.
 * 
 * To contact me, please visit my blog at http://www.cuttingedge.it/blogs/steven/ 
 *
 * Copyright (c) 2009 S. van Deursen
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CuttingEdge.ServiceLayerHelpers.Tests.Unit
{
    [TestClass]
    public class EntityFilterTests
    {
        [TestMethod]
        public void AsQueryable_Always_ReturnsAValue()
        {
            // Act
            var filter = EntityFilter<Person>.AsQueryable();

            // Assert
            Assert.IsNotNull(filter, "AsQueryable should return a value.");
        }

        [TestMethod]
        public void Where_WithValidPredicate_ReturnsAValue()
        {
            // Act
            var filter = EntityFilter<Person>.Where(p => p.Id < 5);

            // Assert
            Assert.IsNotNull(filter, "Where should never return null.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Where_WithNullPredicate_ThrowsException()
        {
            // Arrange
            Expression<Func<Person, bool>> predicate = null;

            // Act
            EntityFilter<Person>.Where(predicate);
        }

        [TestMethod]
        public void AsQueryable_Always_DoesNotFilter()
        {
            // Arrange
            var collection = (new Person[]
            {
                new Person { Id = 2 },
                new Person { Id = 1 }
            }).AsQueryable();

            var filter = EntityFilter<Person>.AsQueryable();

            // Act
            var stillUnfilteredCollection = filter.Filter(collection);

            // Assert
            Assert.AreEqual(2, stillUnfilteredCollection.Count(), "AsQueryable should not filter.");
        }

        [TestMethod]
        public void Where_WithValidPredicate_FiltersCorrectly()
        {
            // Arrange
            var collection = (new Person[]
            {
                new Person { Id = 2 },
                new Person { Id = 1 }
            }).AsQueryable();

            Expression<Func<Person, bool>> predicate = p => p.Id < 2;

            var filter = EntityFilter<Person>.Where(predicate);

            // Act
            var filteredCollection = filter.Filter(collection);

            // Assert
            Assert.AreEqual(1, filteredCollection.Count());
            Assert.AreEqual(1, filteredCollection.First().Id, 1);
        }

        [TestMethod]
        public void Where_CalledMultipleTimes_FiltersCorrectly()
        {
            // Arrange
            var collection = (new Person[]
            {
                new Person { Id = 1 },
                new Person { Id = 2 },
                new Person { Id = 3 }
            }).AsQueryable();

            var filter =
                from person in EntityFilter<Person>.AsQueryable()
                where person.Id > 1
                where person.Id < 3
                select person;

            // Act
            var filteredCollection = filter.Filter(collection);

            // Assert
            Assert.AreEqual(1, filteredCollection.Count());
            Assert.AreEqual(2, filteredCollection.First().Id, "The filter filtered incorrectly.");
        }

        [TestMethod]
        public void ToString_OnAsQueryable_ShouldReturnAValue()
        {
            // Act
            string value = EntityFilter<Person>.AsQueryable().ToString();

            // Assert
            Assert.IsNotNull(value);
        }
    }
}
